package com.test.nnooolwdeaer.util_wooden.http_wooden;

import static com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden.Country_Word_wooden;
import static com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden.Upload_Info_Url_wooden;
import static com.test.nnooolwdeaer.constant_wooden.Url_wooden.*;

import android.text.TextUtils;

import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.FaceUploadActivity2_Viet;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.OcrUploadActivity_Viet;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.SimInfoActivity_Viet;
import com.test.nnooolwdeaer.bean_wooden.star_dialog.StarWindowConfig_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.OcrInfo_Viet_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.dialog_wooden.TipsDialog_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.MainActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.ServiceFeedbackActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseInfoActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoginActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.BindBankAccountActivity_Cote_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.AddPaystackBankAccountActivity_Niri_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.LoanApplyActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.LoanMainActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden.RepayPaystackActivity_Niri_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.login_wooden.LoginPhoneNumActivity_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.GlobalConfig_wooden;
import com.test.nnooolwdeaer.bean_wooden.process_wooden.ProcessInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.CoteBankAccountList_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.LoanApplyDetailInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.LoanLimitInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.LoginInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.RepayH5Info_gn_cl_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.RepayH5Info_ug_ke_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.RepayInfo_wooden;
import com.test.nnooolwdeaer.dialog_wooden.LoadingDialog_wooden;
import com.test.nnooolwdeaer.util_wooden.analytics_wooden.event_wooden.IAnalyticsEvent_wooden;
import com.test.nnooolwdeaer.util_wooden.analytics_wooden.util_wooden.AnalyticsUtil_wooden;

import org.json.JSONArray;
import org.json.JSONObject;

public class HttpPresenter_wooden {

    private static class Inner_wooden {
        private static final HttpPresenter_wooden instance = new HttpPresenter_wooden();
    }

    private HttpPresenter_wooden() {}

    public static HttpPresenter_wooden getInstance_wooden() {
        return Inner_wooden.instance;
    }

    private BaseActivity_wooden activity_wooden;

    public LoadingDialog_wooden progressBarDialog_wooden;

    public void setActivity_wooden(BaseActivity_wooden activity_wooden) {
        if (progressBarDialog_wooden != null && progressBarDialog_wooden.isShowing()) {
            progressBarDialog_wooden.dismiss();
        }
        if (activity_wooden != null) progressBarDialog_wooden = new LoadingDialog_wooden(activity_wooden);
        this.activity_wooden = activity_wooden;
    }

    public void progressBarDialogShow_wooden() {
        if (progressBarDialog_wooden == null) return;
        progressBarDialog_wooden.show();
    }

    public void progressBarDialogDismiss_wooden() {
        if (progressBarDialog_wooden == null) return;
        progressBarDialog_wooden.dismiss();
    }

    public void showToastOnUiThread_wooden(String text) {
        if (activity_wooden != null) {
            activity_wooden.showToastOnUiThread_wooden(text);
        }
    }

    //通知Activity处理响应
    public void notifyActivityHandleResponse_wooden(String word, JSONObject responseJson) {
        if (activity_wooden == null) return;
        try {
            String gcode = responseJson.optString("code");
            String gmsg = responseJson.optString("msg");
            Object gobj = responseJson.opt("obj");

            if (StringUtil_wooden.safeParseInt_wooden(gcode)!=0) {

                gmsg = StringUtil_wooden.getSafeString_wooden(gmsg);

                if (TextUtils.isEmpty(gmsg)) return;

                if (PATH_USER_LOGIN.equalsIgnoreCase(word) || PATH_SMS_CODE.equalsIgnoreCase(word)){
                    new TipsDialog_wooden(activity_wooden,null,gmsg).show();
                    return;
                }

                activity_wooden.showToast_wooden(gmsg);
                return;
            }

            //其它code
            if (gobj instanceof JSONObject) {
                JSONObject objJsonObject = (JSONObject) gobj;
                if (word.equalsIgnoreCase(Upload_Info_Url_wooden)||("/"+ Country_Word_wooden +word).equalsIgnoreCase(Upload_Info_Url_wooden)){
                    if (activity_wooden instanceof BaseInfoActivity_wooden) ((BaseInfoActivity_wooden) activity_wooden).onInfoUploadResponse_wooden();
                    return;
                }
                switch (word) {
//                    case PATH_APP_VERSION://查询App版本更新
//                        AppVersionInfo.parse(objJsonObject);
//                        if (activity instanceof MainActivity) ((MainActivity) activity).onQueryAppVersionResponse();
//                        break;
                    case PATH_GLOBAL://global配置
                        GlobalConfig_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof MainActivity_wooden) ((MainActivity_wooden) activity_wooden).onGlobalResponse_wooden();
                        break;
                    case PATH_APP_SHOW_INFO://获取配置信息
                        AppShowInfo_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof LoginPhoneNumActivity_wooden) ((LoginPhoneNumActivity_wooden) activity_wooden).onAppShowInfoResponse_wooden();
                        else if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onAppShowInfoResponse_wooden();
                        break;
                    case PATH_SMS_CODE://获取验证码
                        if (activity_wooden instanceof BaseLoginActivity_wooden) ((BaseLoginActivity_wooden) activity_wooden).onRequestOtpCodeResponse_wooden(objJsonObject);
                        break;
                    case PATH_USER_LOGIN://用户登录
                        LoginInfo_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof BaseLoginActivity_wooden) ((BaseLoginActivity_wooden) activity_wooden).onUserLoginResponse_wooden();
                        break;
                    case PATH_FEED_BACK://提交反馈
                        if (activity_wooden instanceof ServiceFeedbackActivity_wooden) ((ServiceFeedbackActivity_wooden) activity_wooden).onSubmitServiceFeedback_wooden();
                        break;
                    case PATH_BIND_BANK_ACCOUNT://新增钱包（科特）
                        if (activity_wooden instanceof BindBankAccountActivity_Cote_wooden) ((BindBankAccountActivity_Cote_wooden) activity_wooden).onBindBankAccountResponse_wooden();
                        break;
                    case PATH_DELETE_BOUND_BANK_ACCOUNT://删除钱包（科特）
                        if (activity_wooden instanceof BindBankAccountActivity_Cote_wooden) ((BindBankAccountActivity_Cote_wooden) activity_wooden).onDeleteBankAccountResponse_wooden();
                        break;
                    case PATH_USER_PROCESS://用户流程状态
                        ProcessInfo_wooden.parse_wooden(objJsonObject.toString());
                        activity_wooden.onUserProcessResponse_wooden();
                        break;
                    case PATH_LOAN_LIMIT://额度信息
                        LoanLimitInfo_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onLoanLimitInfoResponse_wooden();
                        break;
                    case PATH_GET_LOAN_APPLY_DETAIL://借款页信息
                        LoanApplyDetailInfo_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onLoanApplyDetailResponse_wooden();
                        else if (activity_wooden instanceof BaseInfoActivity_wooden) ((BaseInfoActivity_wooden) activity_wooden).onLoanApplyDetailResponse_wooden();
                        else if (activity_wooden instanceof LoanApplyActivity_wooden) ((LoanApplyActivity_wooden) activity_wooden).onLoanApplyDetailResponse_wooden();
                        break;
                    case PATH_LOAN_APPLY://发起借款申请响应
//                        AFInstallHelper.getInstance().saveEventInfo(AFInstallHelper.AFEvent.AT_LOAN_APPLY);
//                        FirebaseHelper.getInstance().saveEventInfo(AFInstallHelper.AFEvent.AT_LOAN_APPLY);
                        AnalyticsUtil_wooden.INSTANCE.addEvent(IAnalyticsEvent_wooden.AT_LOAN_APPLY);
                        if (activity_wooden instanceof LoanApplyActivity_wooden) ((LoanApplyActivity_wooden) activity_wooden).onLoanApplyResponse_wooden();
                        break;
                    case PATH_REPAY_INFO://还款信息展示
                        RepayInfo_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onRepayDetailResponse_wooden();
                        break;
                    case PATH_ORDER_REPAY_H5://获取还款Url（加纳、科特）
                        RepayH5Info_gn_cl_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onOrderRepayH5Response_wooden(RepayH5Info_gn_cl_wooden.getInstance_wooden().getUrl_wooden());
                        break;
                    case PATH_GET_ORDER_REPAY_H5://获取还款Url（乌干达、肯尼亚）
                        RepayH5Info_ug_ke_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onOrderRepayH5Response_wooden(RepayH5Info_ug_ke_wooden.getInstance().getUrl_wooden());
                        break;
                    case PATH_ORDER_REPAY_SDK://线上还款-初始化paystack订单（尼日）
                        if (activity_wooden instanceof AddPaystackBankAccountActivity_Niri_wooden) ((AddPaystackBankAccountActivity_Niri_wooden) activity_wooden).onOrderRepaySDKResponse_wooden(objJsonObject);
                        else if (activity_wooden instanceof RepayPaystackActivity_Niri_wooden) ((RepayPaystackActivity_Niri_wooden) activity_wooden).onOrderRepaySDKResponse_wooden(objJsonObject);
                        break;
                    case PATH_CREATE_VIRTUAL_ACCOUNT://线下还款-创建虚拟账号还款（尼日）
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onInitOfflinePayChannelResponse_wooden();
                        break;
                    case Path_Get_Discern_Icr_Info://获取证件Ocr信息（越南）
                        OcrInfo_Viet_wooden.parse_wooden(objJsonObject);
                        if (activity_wooden instanceof OcrUploadActivity_Viet) ((OcrUploadActivity_Viet)activity_wooden).onGetDiscernIcrInfoResponse();
                        break;
                    case Path_Face_Recognition_Check://上传人脸照片信息响应(越南)
                        if (activity_wooden instanceof BaseInfoActivity_wooden) ((BaseInfoActivity_wooden) activity_wooden).onInfoUploadResponse_wooden();
                        break;
                    case Path_Perator_Information://获取运营商链接（越南）
                        if (activity_wooden instanceof SimInfoActivity_Viet) ((SimInfoActivity_Viet)activity_wooden).onPeratorInfoResponse(objJsonObject.optString("url"));
                        break;
                    case Path_Save_Perator_Information://保存运营商认证信息(越南)
                        if (activity_wooden instanceof BaseInfoActivity_wooden) ((BaseInfoActivity_wooden) activity_wooden).onInfoUploadResponse_wooden();
                        break;
                    case PATH_UPLOAD_IMAGE_FILE://图片上传响应
                        String url = objJsonObject.optString("url");
                        if (activity_wooden instanceof OcrUploadActivity_Viet) ((OcrUploadActivity_Viet)activity_wooden).onPhotoUploadResponse(url);
                        else if (activity_wooden instanceof FaceUploadActivity2_Viet) ((FaceUploadActivity2_Viet)activity_wooden).onPhotoUploadResponse(url);
                        break;
                    case PATH_FETCH_STAR_WINDOW_CONFIG:
                        StarWindowConfig_wooden.parseData(objJsonObject);
                        if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden)activity_wooden).onStarWindowConfigResponse();
                        break;
                    case PATH_DROP_DATA:
                        if ("GET".equalsIgnoreCase(HttpManager_wooden.dropType)){
                            String showDropButton = objJsonObject.optString("showDropButton");
                            if (activity_wooden instanceof  LoanMainActivity_wooden loanMainActivity)
                                loanMainActivity.onDropConfigResponse("YES".equalsIgnoreCase(showDropButton) || "true".equalsIgnoreCase(showDropButton));
                        }else if ("DROP".equalsIgnoreCase(HttpManager_wooden.dropType)) activity_wooden.onDrop();
                        break;
                    default:
                        //throw new IllegalStateException("Unexpected value: " + word);
                }
            } else if (gobj instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) gobj;
                switch (word) {
                    case PATH_FETCH_BOUND_BANK_ACCOUNT://获取钱包列表（科特）
                        CoteBankAccountList_wooden.parse_wooden(jsonArray);
                        if (activity_wooden instanceof BindBankAccountActivity_Cote_wooden) ((BindBankAccountActivity_Cote_wooden) activity_wooden).onFetchBankAccountListResponse_wooden();
                        else if (activity_wooden instanceof LoanMainActivity_wooden) ((LoanMainActivity_wooden) activity_wooden).onFetchBankAccountListResponse_wooden();
                        else if (activity_wooden instanceof LoanApplyActivity_wooden) ((LoanApplyActivity_wooden) activity_wooden).onFetchBankAccountListResponse_Cote_wooden();
                        break;
                }
            } else {
                //其它类型
                gmsg = StringUtil_wooden.getSafeString_wooden(gmsg);
                if (TextUtils.isEmpty(gmsg)) {
                    gmsg = "Response something error";
                }
                activity_wooden.showToast_wooden(gmsg);
            }
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

}
